<template>
  <div class="app-container">
    <div class="top" style="margin-top: 28px;">
      <el-input
        v-model="kwInput"
        placeholder="输入 IP 进行筛选"
        class="input-with-select"
        clearable
        @keyup.native.enter="handleSearch"
      >
        <el-button slot="append" icon="el-icon-search" :disabled="listLoading" @click="getList">搜索</el-button>
      </el-input>
    </div>
    <el-table v-loading.body="listLoading" :data="data" border fit highlight-current-row style="width: 100%" @sort-change="handleSort">
      <el-table-column align="center" label="服务器列表" prop="servers" sortable="custom" width="300">
        <template slot-scope="scope">
          <el-dropdown v-if="scope.row.servers.length" trigger="click">
            <span class="el-dropdown-link">
              {{ scope.row.servers[0].gsapi_ip }}<i class="el-icon-caret-bottom el-icon--right" />
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item v-for="(item,index) in scope.row.servers" :key="index" class="clearfix">
                <span>{{ item.gsapi_ip }}</span>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
      <el-table-column align="center" label="ip" prop="ip" width="260" sortable="custom" />
      <el-table-column align="center" label="count" prop="count" width="200" sortable="custom" />
      <el-table-column align="center" label="更新时间" prop="sync_at" sortable="custom" />
    </el-table>
    <el-pagination
      v-if="data.length"
      background
      class="mt-pagination"
      :current-page="page"
      :page-sizes="[10,20,50,100]"
      :page-size="pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handlePageChange"
    />
  </div>
</template>

<script>
import { Get } from '@/api/orders/serverconnections'
import qs from 'qs'
const orderMapper = {
  'ascending': 0,
  'descending': 1,
  'null': ''
}
export default {
  name: 'Serverconnections',
  data() {
    return {
      data: [],
      action: '',
      title: '修改配置',
      websites: [],
      listLoading: true,
      // 输入
      kwInput: '',
      // pagination
      pagesize: 10,
      page: 1,
      total: 0,
      order_by: 'count',
      order_desc: '1'
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      Get(null, qs.stringify({
        kw: this.kwInput,
        pagesize: this.pagesize,
        order_by: this.order_by,
        order_desc: this.order_desc,
        page: this.page
      }))
        .then(response => {
          this.data = response.data.data
          this.listLoading = false
          this.total = response.data.total
        }).catch(err => {
          console.log(err)
          this.listLoading = false
        })
    },
    notify(message, duration) {
      this.$notify({
        title: '成功',
        message: message,
        type: 'success',
        duration: duration || 2000
      })
    },
    // pageSize 改变后触发 table 更新
    handleSizeChange(val) {
      this.pagesize = val
      this.getList()
    },
    // page 改变后触发 table 更新
    handlePageChange(val) {
      this.page = val
      this.getList()
    },
    handleSort(v) {
      this.order_by = v.prop
      this.order_desc = orderMapper[v.order]
      this.getList()
    }
  }
}
</script>

<style scoped>
  .el-tag + .el-tag {
    margin-left: 10px;
  }

  .top >>> .el-input {
    width: 80%;
  }

  .top >>> .input-with-select .el-input-group__prepend {
    background-color: #fff;
  }

  .app-container >>> .el-transfer-panel {
    width: 350px !important;
  }

  .top {
    padding-left: 20px;
    margin-bottom: 5px;
    margin-top: 28px;
  }

  .add-btn {
    float: right;
    margin-right: 20px;
    width: 150px
  }

  .mt-pagination {
    float: right;
    margin-top: 5px;
  }

  .app-container >>> .el-dialog {
    margin-top: 48px !important;
    margin-bottom: 5px;
  }

  .mt-form >>> .el-select {
    width: 100%;
  }

  .mt-form >>> .el-input__inner {
    width: 100%;
  }

  .mt-dialog >>> .el-dialog {
    margin: 10px auto !important;
  }

  .mt-dialog >>> .el-dialog__body {
    padding-bottom: 60px;
    padding-top: 5px;
  }

</style>
